package com.xing.lei.happy.thrid.flh;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * Description:
 *
 * @author flh
 * @version 1.00
 * @Date 2023/2/27
 */
public class GetPinYin {

    public static void main(String[] args) {
        String[] strs = {"环境暴露史","家族史","婴幼儿呼吸疾病史"};
        for (String str : strs) {
            System.out.println(getFirstLettersUpper(str));
        }
    }

    /**
     * 将中文转为汉语拼音，提取每个字首字母并大写
     * @param ChineseLanguage
     * @return
     */
    public static String getLettersUpper(String ChineseLanguage){
        return getLetters(ChineseLanguage ,HanyuPinyinCaseType.UPPERCASE, false);
    }

    /**
     * 将中文转为汉语拼音，提取每个字首字母并小写
     * @param ChineseLanguage
     * @return
     */
    public static String getLettersLower(String ChineseLanguage){
        return getLetters(ChineseLanguage ,HanyuPinyinCaseType.LOWERCASE, false);
    }

    /**
     * 将中文转为汉语拼音，提取每个字首字母并大写
     * @param ChineseLanguage
     * @return
     */
    public static String getFirstLettersUpper(String ChineseLanguage){
        return getLetters(ChineseLanguage ,HanyuPinyinCaseType.UPPERCASE, true);
    }

    /**
     * 将中文转为汉语拼音，提取每个字首字母并小写
     * @param ChineseLanguage
     * @return
     */
    public static String getFirstLettersLower(String ChineseLanguage){
        return getLetters(ChineseLanguage ,HanyuPinyinCaseType.LOWERCASE, true);
    }

    private static String getLetters(String ChineseLanguage, HanyuPinyinCaseType caseType, boolean firstFlag) {
        char[] cl_chars = ChineseLanguage.trim().toCharArray();
        String hanyupinyin = "";
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(caseType);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
        try {
            for (int i = 0; i < cl_chars.length; i++) {
                String str = String.valueOf(cl_chars[i]);
                if (str.matches("[\u4e00-\u9fa5]+")) {
                    if(firstFlag){
                        // 如果字符是中文,则将中文转为汉语拼音,并取第一个字母
                        hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0].substring(0, 1);
                    }else{
                        hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0];
                    }
                } else if (str.matches("[0-9]+")) {// 如果字符是数字,取数字
                    hanyupinyin += cl_chars[i];
                } else if (str.matches("[a-zA-Z]+")) {// 如果字符是字母,取字母
                    hanyupinyin += cl_chars[i];
                } else {// 否则不转换
                    hanyupinyin += cl_chars[i];//如果是标点符号的话，带着
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            System.out.println("字符不能转成汉语拼音");
        }
        return hanyupinyin;
    }
}
